import java.util.ArrayList;
import java.util.List;

//全排列
//测试链接：https://leetcode.cn/problems/permutations/
public class Permute {
    List<List<Integer>> ret;
    List<Integer> path;
    boolean[] check;

    public List<List<Integer>> permute(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        check = new boolean[nums.length];
        dfs(nums);
        return ret;
    }

    public void dfs(int[] nums) {
        if (nums.length == path.size()) {
            ret.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (check[i] == false) {
                path.add(nums[i]);
                check[i] = true;
                dfs(nums);
                // 回溯 -> 恢复现场
                check[i] = false;
                path.remove(path.size() - 1);
            }
        }
    }
}
